<?php
// 角色模块
class RoleAction extends CommonAction {
	function _filter(&$map){
		$map['name'] = array('like',"%".$_POST['name']."%");
	}
     /**
     +----------------------------------------------------------
     * 增加组操作权限
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function setApp()
    {
        $menu_group_ck_ids   = explode(',',$_POST['group_ck_ids']);//被选中的菜单组的ID
		$groupId		= $_POST['groupId'];//当前要分配权限组的组ID
		$models			= '';//格式:model_ck_
		$opt			= '';//格式:opt_ck_
		$Role   		= D('Role');
		$prea 			=$_POST;
		$level			=2;//目前设想的是保存模块的id
		$pid  			=1;//
		$status			=1;
		$DB_PREFIX=C('DB_PREFIX');
		/**
		 * 目前的做法不是很妥当，先删除以前的该权限分组的所有数据，然后在下面重新插入数据  步骤：1将之前的数据update成不可用状态,执行成功后，在删除
		 */
		$sql='Update '.$DB_PREFIX.'access set status =-1 where role_id='.$groupId;
		$Role->saveAccess($sql);//执行更新操作将之前的数据执行成不可用
		$result=true;//
		foreach ($menu_group_ck_ids as $g){
			if(is_array($prea['model_ck_'.$g])){
				foreach ($prea['model_ck_'.$g] as $m){
					$opt='';List($node_id,$modele)=explode('_',$m);
					if(is_array($prea['opt_ck_'.$node_id])){
						foreach ($prea['opt_ck_'.$node_id] as $o){
							empty($opt)?$opt=','.$o.',':$opt.=$o.','; 
						}
				    	$sql='INSERT INTO '.$DB_PREFIX.'access (role_id,node_id, group_id, level,  pid,  module,  opt,status)
				    	values
				    	('.$groupId.','.$node_id.','.$g.','.$level.','.$pid.',"'.$modele.'","'.$opt.'",'.$status.')';
				    	if(!$Role->saveAccess($sql)){
				    	   Log::write("权限数据保存失败:\r\n".$sql,log::ERR,log::FILE,LOG_PATH.'/'.date("Ymd",time()).'_test_log.txt');
				    	   $result=false;
				    	}
					}
				}
			}
		}
		//Log::write("sqllll:".$sql,log::ERR,log::FILE,LOG_PATH.'/'.date("Ymd",time()).'_test_log.txt');
		if($result===false) {
			$this->error('权限分配失败！');
		}else {
			$sql='DELETE from '.$DB_PREFIX.'access where status =-1 and role_id='.$groupId;
			$Role->saveAccess($sql);
			$this->success('权限分配成功！');
		}
		
    }


    /**
     +----------------------------------------------------------
     * 组操作权限列表
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function app()
    {   
    	$DB_PREFIX=C('DB_PREFIX');
        //读取系统的菜单分组列表
        $Group   =  D("Group");
        $list	=	$Group->where('status=1 and `show`=0')->field('id,`name`,title')->findAll();
		$GroupList=Array();//菜单的分组
        foreach ($list as $vo){
			$GroupList[$vo['id']]['group_name']	=	$vo['title'];
			$GroupList[$vo['id']]['group_id']	=	$vo['id'];
		}

        //读取系统模块列表//level=2 模块的level都是等于2
		$Node   =  D('Node');  
        $list       =  $Node->where('status=1 and level =2 and group_id>0')->field('id,`name`,title,pid,level,group_id')->findAll();
        $NodeList=Array();
        foreach ($GroupList as $k =>$v){
        	$NodeList='';
			foreach ($list as $vo){
				if($vo['group_id']==$k){
					$NodeList[]	  =	$vo;
				}
			}
			$GroupList[$k]['models']=$NodeList;
        }
        
        //读取系统模块的操作类型列表//level=3 ,pid等于2
        $list       =  $Node->where('status=1 and level =3 and group_id=0')->field('id,`name`,title,pid,level,group_id')->findAll();
        $optList=Array();
        foreach ($list as $k =>$v){
			$optList[]    = $v;
        }
        $Access   =  D('Access'); 
        $where='`status`=1 and role_id=2';
        //读取该权限分组的菜单分组权限
        $menu_group_id=array();
        $menu_group_id=$Access->query('select  group_id from '.$DB_PREFIX.'access where '.$where.' group by group_id');
        //读取该权限分组的模块分组权限
        $models_group_id=array();
        $models_group_id=$Access->query('select  node_id,opt from '.$DB_PREFIX.'access where '.$where.' group by node_id');
        //Log::write("sqllll:".'select  node_id,opt from xp_access where '.$where.' group by node_id',log::ERR,log::FILE,LOG_PATH.'/'.date("Ymd",time()).'_test_log.txt');
        //读取该权限分组的操作分组权限
        //$models_group_id=array();
        //$models_group_id=$Access->query('select  node_id from xp_access where '.$where.' group by node_id');

		/*******生成权限分配菜单******************************************************************************/
		$tp='<table width="100%" align="left">';
		foreach ($GroupList as $k =>$v){
			   $checked1='';
			   foreach ($menu_group_id as $k1 =>$v1){//这里需要优化
			    if($v['group_id']==$v1['group_id'])$checked1='checked="checked"';
			   }
			   $tp.='<tr><td colspan="2" style="font-size:14px; color:#FF3300; font-weight:bold;">
			         <input type="checkbox" name="group_ck" '.$checked1.' value="'.$v['group_id'].'" onclick=set_group_ck_hidden("group_ck",this.value) />'.$v['group_name'].'</td></tr>';
				foreach ($v['models'] as $mk =>$mv){
					    $checked1='';
						foreach ($models_group_id as $k1 =>$v1){//这里需要优化
					    if($mv['id']==$v1['node_id'])$checked1='checked="checked"';
					    }
				     $tp.='<tr style="height:30px;">
					      <td style="border-bottom:1px dashed #CCCCCC">
					      <input type="checkbox" name="model_ck_'.$v['group_id'].'[]" '.$checked1.' onclick=set_child_checked(this,"'.$mv['id'].'") value="'.$mv['id'].'_'.$mv['name'].'"/>'.$mv['title'].'</td>
					      <td style="border-bottom:1px dashed #CCCCCC">';
				     foreach ($optList as $ok =>$ov){
				     	  $checked1='';
				     	if($ov['pid']==0 || $ov['pid']==$mv['id']){
				          foreach ($models_group_id as $k1 =>$v1){//这里需要优化
				          	$models_opt=explode(",", $v1['opt']);
					        if($mv['id']==$v1['node_id'] && in_array($ov['name'],$models_opt))$checked1='checked="checked"';
					      }
					      $tp.='<input type="checkbox" name="opt_ck_'.$mv['id'].'[]" '.$checked1.' value="'.$ov['name'].'" />'.$ov['title'].',';
					 
				     	}  
				     }	  
					   $tp.=   '</td></tr>';
				}
				$tp.='
				      <tr>
					      <td colspan="2" style="border-bottom:1px solid #CCCCCC">&nbsp;</td>
					  </tr>
				';
		}
		$tp.='</table>';
		
		$this->assign('tp',$tp);
					  
		$menu_group='';		
        foreach ($menu_group_id as $k1 =>$v1){//这里需要优化
			   if($k1==0){
			     $menu_group=','.$v1['group_id'].',';
			   }else{
			     $menu_group.=$v1['group_id'].',';
			   }
		}
		$this->assign('menu_group',$menu_group);
		
        //获取当前用户组项目权限信息
       /** $groupId =  isset($_GET['groupId'])?$_GET['groupId']:'';
		$groupAppList = array();
		if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
			//获取当前组的操作权限列表
            $list	=	$group->getGroupAppList($groupId);
			foreach ($list as $vo){
				$groupAppList[$vo['id']]	=	$vo['id'];
			}
		}
		$this->assign('groupAppList',$groupAppList);
        $this->assign('appList',$appList);**/
        $this->display();

        return;
    }

     /**
     +----------------------------------------------------------
     * 增加组操作权限
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function setModule()
    {
        $id     = $_POST['groupModuleId'];
		$groupId	=	$_POST['groupId'];
        $appId	=	$_POST['appId'];
		$group    =   D("Role");
		$group->delGroupModule($groupId,$appId);
		$result = $group->setGroupModules($groupId,$id);

		if($result===false) {
			$this->error('模块授权失败！');
		}else {
			$this->success('模块授权成功！');
		}
    }


    /**
     +----------------------------------------------------------
     * 组操作权限列表
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function module()
    {
        $groupId =  $_GET['groupId'];
        $appId  = $_GET['appId'];

		$group   =  D("Role");
        //读取系统组列表
        $list=$group->field('id,name')->findAll();
		foreach ($list as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

        if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
            //读取系统组的授权项目列表
            $list	=	$group->getGroupAppList($groupId);
			foreach ($list as $vo){
				$appList[$vo['id']]	=	$vo['title'];
			}
            $this->assign("appList",$appList);
        }
        $node    =  D("Node");
        if(!empty($appId)) {
            $this->assign("selectAppId",$appId);
        	//读取当前项目的模块列表
			$where['level']=2;
			$where['pid']=$appId;
            $nodelist=$node->field('id,title')->where($where)->findAll();
			foreach ($nodelist as $vo){
				$moduleList[$vo['id']]	=	$vo['title'];
			}
        }

        //获取当前项目的授权模块信息
		$groupModuleList = array();
		if(!empty($groupId) && !empty($appId)) {
            $grouplist	=	$group->getGroupModuleList($groupId,$appId);
			foreach ($grouplist as $vo){
				$groupModuleList[$vo['id']]	=	$vo['id'];
			}
		}

		$this->assign('groupModuleList',$groupModuleList);
        $this->assign('moduleList',$moduleList);

        $this->display();

        return;
    }

     /**
     +----------------------------------------------------------
     * 增加组操作权限
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function setAction()
    {
        $id     = $_POST['groupActionId'];
		$groupId	=	$_POST['groupId'];
        $moduleId	=	$_POST['moduleId'];
		$group    =   D("Role");
		$group->delGroupAction($groupId,$moduleId);
		$result = $group->setGroupActions($groupId,$id);

		if($result===false) {
			$this->error('操作授权失败！');
		}else {
			$this->success('操作授权成功！');
		}
    }


    /**
     +----------------------------------------------------------
     * 组操作权限列表
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function action()
    {
        $groupId =  $_GET['groupId'];
        $appId  = $_GET['appId'];
        $moduleId  = $_GET['moduleId'];

		$group   =  D("Role");
        //读取系统组列表
        $grouplist=$group->field('id,name')->findAll();
		foreach ($grouplist as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

        if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
            //读取系统组的授权项目列表
            $list	=	$group->getGroupAppList($groupId);
			foreach ($list as $vo){
				$appList[$vo['id']]	=	$vo['title'];
			}
            $this->assign("appList",$appList);
        }
        if(!empty($appId)) {
            $this->assign("selectAppId",$appId);
        	//读取当前项目的授权模块列表
            $list	=	$group->getGroupModuleList($groupId,$appId);
			foreach ($list as $vo){
				$moduleList[$vo['id']]	=	$vo['title'];
			}
            $this->assign("moduleList",$moduleList);
        }
        $node    =  D("Node");

        if(!empty($moduleId)) {
            $this->assign("selectModuleId",$moduleId);
        	//读取当前项目的操作列表
			$map['level']=3;
			$map['pid']=$moduleId;
            $list	=	$node->where($map)->field('id,title')->findAll();
			if($list) {
				foreach ($list as $vo){
					$actionList[$vo['id']]	=	$vo['title'];
				}
			}
        }


        //获取当前用户组操作权限信息
		$groupActionList = array();
		if(!empty($groupId) && !empty($moduleId)) {
			//获取当前组的操作权限列表
            $list	=	$group->getGroupActionList($groupId,$moduleId);
			if($list) {
			foreach ($list as $vo){
				$groupActionList[$vo['id']]	=	$vo['id'];
			}
			}

		}

		$this->assign('groupActionList',$groupActionList);
		//$actionList = array_diff_key($actionList,$groupActionList);
        $this->assign('actionList',$actionList);

        $this->display();

        return;
    }

    /**
     +----------------------------------------------------------
     * 增加组操作权限
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function setUser()
    {
        $id     = $_POST['groupUserId'];
		$groupId	=	$_POST['groupId'];
		$group    =   D("Role");
		$group->delGroupUser($groupId);
		$result = $group->setGroupUsers($groupId,$id);
		if($result===false) {
			$this->error('授权失败！');
		}else {
			$this->success('授权成功！');
		}
    }

    /**
     +----------------------------------------------------------
     * 组操作权限列表
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    public function user()
    {
        //读取系统的用户列表
        $user    =   D("User");
		$list2=$user->field('id,account,nickname')->findAll();
		//echo $user->getlastsql();
		//dump(	$user);
		foreach ($list2 as $vo){
			$userList[$vo['id']]	=	$vo['account'].' '.$vo['nickname'];
		}

		$group    =   D("Role");
        $list=$group->field('id,name')->findAll();
		foreach ($list as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

        //获取当前用户组信息
        $groupId =  isset($_GET['id'])?$_GET['id']:'';
		$groupUserList = array();
		if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
			//获取当前组的用户列表
            $list	=	$group->getGroupUserList($groupId);
			foreach ($list as $vo){
				$groupUserList[$vo['id']]	=	$vo['id'];
			}

		}
		$this->assign('groupUserList',$groupUserList);
        $this->assign('userList',$userList);
        $this->display();

        return;
    }
    
	public function _before_edit(){
	   $Group = D('Role');
        //查找满足条件的列表数据
        $list     = $Group->field('id,name')->findAll();
        $this->assign('list',$list);

	}
	
	public function _before_add(){
	   $Group = D('Role');
        //查找满足条件的列表数据
        $list     = $Group->field('id,name')->findAll();
        $this->assign('list',$list);

	}
	
    public function select()
    {
        $map = $this->_search();
        //创建数据对象
        $Group = D('Role');
        //查找满足条件的列表数据
        $list     = $Group->field('id,name')->findAll();
        $this->assign('list',$list);
        $this->display();
        return;
    }
}